package com.geline.mybatisplus.handler;

import cn.hutool.core.util.StrUtil;
import com.geline.mybatisplus.annotation.ExtractStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @ExtractStatus 处理器
 *
 * @author mx
 * @date 2025/9/20
 */
@Slf4j
public class ExtractStatusHandler {

    public void handle(Object entity) {
        BeanMap entityMap = BeanMap.create(entity);
        Map<String, Field> statusMap = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for(Field field : fields){
            ExtractStatus[] statuses = field.getAnnotationsByType(ExtractStatus.class);
            if(statuses != null && statuses.length>0){
                for(ExtractStatus status : statuses){
                    String totalKey = StrUtil.toCamelCase(status.totalKey());
                    Object value = entityMap.get(totalKey);
                    if(value!=null){
                        //只添加与子表相关的状态字段
                        statusMap.put(field.getName(),  field);
                    }else {
                        log.warn("@ExtractStatus 缺少 totalKey 值: {}", totalKey);
                    }
                }
            }
        }
        log.debug("@ExtractStatus statusMap ={}", statusMap.keySet());

        Map<String, Object> updateValue = new HashMap<>();
        //再更新状态字段：比较总数与完成数
        for(String fieldName : statusMap.keySet()){
            Field statusField = statusMap.get(fieldName);
            ExtractStatus extractStatus = statusField.getAnnotation(ExtractStatus.class);
            String totalKey = extractStatus.totalKey();
            String overKey = extractStatus.overKey();
            //SELECT count(id) from t_sale_detail where sale_id=#{id}
            Object totalValue = entityMap.get(StrUtil.toCamelCase(totalKey));//1
            //select count(id) from t_sale_detail where sale_id=#{id} and binding_status='2'
            Object overValue = entityMap.get(StrUtil.toCamelCase(overKey));//0

            log.info(">>>>@ExtractStatus>>>> overValue={}, totalValue={}", overValue, totalValue);
            if(overValue==null || Double.valueOf(overValue.toString())==0){
                //更新主表状态为：待处理
                entityMap.put(fieldName, extractStatus.initValue());
                updateValue.put(fieldName, extractStatus.initValue());
            }else if(totalValue!=null && overValue!=null && Double.valueOf(overValue.toString())>=Double.valueOf(totalValue.toString())){
                //更新主表状态为：已完成
                entityMap.put(fieldName, extractStatus.overValue());
                updateValue.put(fieldName, extractStatus.overValue());
            }else{
                //更新主表状态为：部分完成
                entityMap.put(fieldName, extractStatus.partValue());
                updateValue.put(fieldName, extractStatus.partValue());
            }
        }
        if(updateValue.size()>0){
            log.info(">>>> updateFieldValue[{}]={}", entity.getClass().getSimpleName(), updateValue);
        }
    }
}
